package com.example.myapplication;

import android.content.Context;

public class UptimeStatsService {
    private final Context context;

    public UptimeStatsService(Context context) {
        this.context = context;
    }

    /**
     * 获取当前开机时长（毫秒）- 使用系统累计时间
     * @return 开机时长
     */
    public long getCurrentUptime() {
        return android.os.SystemClock.elapsedRealtime();
    }

    /**
     * 获取开机时间（计算得出）
     * @return 开机时间戳
     */
    public long getBootTime() {
        return System.currentTimeMillis() - getCurrentUptime();
    }

    /**
     * 格式化时长显示
     * @param milliseconds 毫秒数
     * @return 格式化的时长字符串
     */
    public static String formatUptime(long milliseconds) {
        if (milliseconds < 0) {
            return "未知";
        }
        
        long seconds = milliseconds / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        
        if (days > 0) {
            return String.format("%d天%d小时%d分钟%d秒", days, hours % 24, minutes % 60, seconds % 60);
        } else if (hours > 0) {
            return String.format("%d小时%d分钟%d秒", hours, minutes % 60, seconds % 60);
        } else if (minutes > 0) {
            return String.format("%d分钟%d秒", minutes, seconds % 60);
        } else {
            return String.format("%d秒", seconds);
        }
    }

}
